package com.education.teacher.controller;


import java.util.ArrayList;
import java.util.Map;

import com.education.model.QuestionExam;
import com.education.service.SelectExamApi;
import com.education.util.ExcelUtil;
import org.springframework.web.bind.annotation.*;

import com.alibaba.dubbo.config.annotation.Reference;

import com.education.model.ResultDo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;

@Api(value = "/api/teacher", description = "判断题题库")
@RestController
@RequestMapping("/api/teacher")
public class CourseSectionController {
    
    @Reference
    private SelectExamApi icsa;
    
    /**
     * 2017/8/7 王娇
     * 判断题录入，分页
     * 章节测试页面数据包括下拉框等控制层
     * @param
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/judgeSection/page", httpMethod = "GET", value = "章节测试题库")
    @RequestMapping(value="/judgeSection/page",method=RequestMethod.GET)
    public ResultDo<Object> getPage(@RequestParam("pageNum") Integer pageNo, Integer pageSize, String searchMessage) throws Exception{

        Map<Object,Object> map = icsa.getPage(pageNo,pageSize,searchMessage,2);
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        result.setResData(map);
        return result;
    }
    
    /**添加章节测试试题
     * @param jcs
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/judgeSection/addjudge", httpMethod = "POST", value = "添加章节测试试题")
    @RequestMapping(value="/judgeSection/addjudge",method=RequestMethod.POST)
    public ResultDo<Object> addJudgeSection(@RequestBody QuestionExam jcs,HttpSession session) throws Exception{
        int tid=Integer.parseInt((String) session.getAttribute("teacherId"));
        jcs.setTeacherId(tid);
        icsa.addCourseSection(jcs);
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        return result;
    }
    
    /**添加期末测试试题
     * @param jcs
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/judgeSection/addExam", httpMethod = "POST", value = "添加期末测试试题")
    @RequestMapping(value="/judgeSection/addExam",method=RequestMethod.POST)
    public ResultDo<Object> addJudgeExam(@RequestBody QuestionExam jcs,HttpSession session) throws Exception{
        int tid=Integer.parseInt((String) session.getAttribute("teacherId"));
        jcs.setTeacherId(tid);
        icsa.addExamService(jcs);
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        return result;
    }
    
    
    
    /**添加入学测试试题
     * @param jcs
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/judgeSection/addEnter", httpMethod = "POST", value = "添加入学测试试题")
    @RequestMapping(value="/judgeSection/addEnter",method=RequestMethod.POST)
    public ResultDo<Object> addJudgeEnter(@RequestBody QuestionExam jcs,HttpSession session) throws Exception{
        int tid=Integer.parseInt((String) session.getAttribute("teacherId"));
        jcs.setTeacherId(tid);
        icsa.addEnterText(jcs);
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        return result;

    
    } 
    /**更新判断题
     * @param jcs
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/judgeSection/update", httpMethod = "POST", value = "根据id查找判断题")
    @RequestMapping(value="/judgeSection/update",method=RequestMethod.POST)
    public ResultDo<Object> updateJudgeCourseSection(@RequestBody QuestionExam jcs) throws Exception{

        icsa.update(jcs);
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        return result;
 
    }  
    /**删除判断题
     * @param jcs
     * @return
     * @throws Exception
     */
    @ApiOperation(notes = "/judgeSection/delete", httpMethod = "POST", value = "根据id查找判断题")
    @RequestMapping(value="/judgeSection/delete",method=RequestMethod.POST)
    public ResultDo<Object> deleteJudgeCourseSection(@RequestBody QuestionExam jcs) throws Exception{

        icsa.delete(jcs.getQuestionId());
        ResultDo<Object> result = new ResultDo<Object>();
        result.setResMsg("请求成功");
        result.setResCode(0);
        return result;

    }

    @ApiOperation(notes = "/judgeSection/batchUpdate", httpMethod = "POST", value = "批量导入判断题")
    @RequestMapping(value = "/judgeSection/batchUpdate",method = RequestMethod.POST)
    public ResultDo<Object> batchAddChoice(MultipartFile excelFile,QuestionExam questionExam, HttpSession session) throws Exception{
        int tid=Integer.parseInt((String) session.getAttribute("teacherId"));
        questionExam.setTeacherId(tid);
        Map<String, ArrayList<String>> map= ExcelUtil.readExcelData(excelFile.getInputStream());

        icsa.excelUplode(map,questionExam);
        ResultDo<Object> resultDo = new ResultDo<Object>();

        resultDo.setResCode(0);
        resultDo.setResMsg("导入成功");
        return resultDo;
    }

}
